package kr.hkit.powerjava.chapter17;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridLayout;

public class SwingComponentCheckBox extends JPanel implements ItemListener{
	JCheckBox[] buBoxs= new JCheckBox[3];
	String[] actors={"image1","image2","image3"};
	JLabel[] acJLabels=new JLabel[3];
	ImageIcon[] icons= new ImageIcon[3];

	public SwingComponentCheckBox() {
		
		initialize();
	}
	private void initialize() {
		setLayout(new GridLayout(0,4));
		for(int i=0;i<3;i++){
			buBoxs[i]= new JCheckBox(actors[i]);
			buBoxs[i].addItemListener(this);
			icons[i]=new ImageIcon(actors[i]+".jpg");
		}
		JPanel chPanel= new JPanel(new GridLayout(0,1));
		for(int i=0;i<3;i++){
			chPanel.add(buBoxs[i]);
		}
			add(chPanel);
			add(acJLabels[0]);
			add(acJLabels[1]);
			add(acJLabels[2]);
		
	}

	@Override
	public void itemStateChanged(ItemEvent e) {
		ImageIcon image=null;
		Object source=e.getItemSelectable();
		for(int i=0;i<3;i++){
			if(source==buBoxs[i]){
				if(e.getStateChange()==ItemEvent.DESELECTED)
					acJLabels[i].setIcon(null);
			}else{
				acJLabels[i].setIcon(icons[i]);
			}
		}
		
	}

}
